package admin

import (
	"chaowan/data_source"
	"chaowan/models"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

type UserlistRBody struct {
	Data       []models.User `json:"data"`
	PageNo     int           `json:"pageNo"`
	PageSize   int           `json:"pageSize"`
	TotalCount int64         `json:"totalCount"`
	TotalPage  int64         `json:"totalPage"`
}

type UserlistBody struct {
	Uid      int    `json:"uid"`
	Laiyuan  int64  `json:"laiyuan"`
	Status   int    `json:"status"`
	Page     int    `json:"page"`
	Pagesize int    `json:"pagesize"`
	Name     string `json:"name"`
	Phone    string `json:"phone"`
	zPhone   string `json:"zphone"`
}

func Userlist(ctx *gin.Context) {
	request := UserlistBody{}
	ctx.ShouldBind(&request)
	status := request.Status
	laiyuan := request.Laiyuan
	name := request.Name
	phone := request.Phone
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.User
	res := data_source.Db
	if name != "" {
		res = res.Where("nickname like ?", "%"+name+"%")
	}
	if status > 0 {
		res = res.Where("status = ?", status)
	}
	if laiyuan > 0 {
		res = res.Where("laiyuan = ?", laiyuan)
	}
	if phone != "" {
		res = res.Where("phone = ?", phone)
	}
	res.Model(&models.User{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := UserlistRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type UserzengjianBody struct {
	Uid      int64   `json:"uid"`
	State    int64   `json:"state"`
	Classify int64   `json:"classify"`
	Price    float64 `json:"price"`
}

func Userzengjian(ctx *gin.Context) {
	request := UserzengjianBody{}
	ctx.ShouldBind(&request)
	classify := request.Classify
	state := request.State
	uid := request.Uid
	price := request.Price

	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "uid不能等于0",
		})
		return
	}
	if price == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "price不能等于0",
		})
		return
	}
	var user models.User
	data_source.Db.Where("id = ?", uid).First(&user)
	if state == 1 {
		if classify == 1 {
			userdaibiadd := models.Userdaibi{
				Uid:     uid,
				Addtime: time.Now().Unix(),
				Status:  1,
				Daibi:   price,
				Title:   "管理员增加矿石",
				State:   8,
				Sydaibi: user.Daibi + price,
			}
			data_source.Db.Create(&userdaibiadd)
			data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("daibi", user.Daibi+price)
		} else if classify == 2 {
			userintegraladd := models.Userintegral{
				Uid:        uid,
				Addtime:    time.Now().Unix(),
				State:      1,
				Integral:   int64(price),
				Title:      "管理员增加积分",
				Syintegral: user.Integral + int64(price),
			}
			data_source.Db.Create(&userintegraladd)
			data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("integral", user.Integral+int64(price))
		} else if classify == 3 {
			usermojingadd := models.Usermojing{
				Uid:      uid,
				Addtime:  time.Now().Unix(),
				State:    1,
				Mojing:   price,
				Title:    "管理员增加魔晶",
				Symojing: user.Mojing + price,
			}
			data_source.Db.Create(&usermojingadd)
			data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("mojing", user.Mojing+price)
		}
	} else {
		if classify == 1 {
			if user.Daibi < price {
				ctx.JSON(http.StatusOK, gin.H{
					"code":    400,
					"error":   1,
					"success": 400,
					"data":    "on",
					"msg":     "可减矿石不足",
				})
				return
			}
			userdaibiadd := models.Userdaibi{
				Uid:     uid,
				Addtime: time.Now().Unix(),
				Status:  2,
				Daibi:   price,
				Title:   "管理员增加减少",
				State:   8,
				Sydaibi: user.Daibi - price,
			}
			data_source.Db.Create(&userdaibiadd)
			data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("daibi", user.Daibi-price)
		} else if classify == 2 {
			if user.Integral < int64(price) {
				ctx.JSON(http.StatusOK, gin.H{
					"code":    400,
					"error":   1,
					"success": 400,
					"data":    "on",
					"msg":     "可减积分不足",
				})
				return
			}
			userintegraladd := models.Userintegral{
				Uid:        uid,
				Addtime:    time.Now().Unix(),
				State:      2,
				Integral:   int64(price),
				Title:      "管理员减少积分",
				Syintegral: user.Integral - int64(price),
			}
			data_source.Db.Create(&userintegraladd)
			data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("integral", user.Integral-int64(price))
		} else if classify == 3 {
			if user.Mojing < price {
				ctx.JSON(http.StatusOK, gin.H{
					"code":    400,
					"error":   1,
					"success": 400,
					"data":    "on",
					"msg":     "可减魔晶不足",
				})
				return
			}
			usermojingadd := models.Usermojing{
				Uid:      uid,
				Addtime:  time.Now().Unix(),
				State:    2,
				Mojing:   price,
				Title:    "管理员减少魔晶",
				Symojing: user.Mojing - price,
			}
			data_source.Db.Create(&usermojingadd)
			data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("mojing", user.Mojing-price)
		}
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
